#include "svgDrawer.h"
#include <math.h>

bool svgDrawer::newImage(const char *file_name, int width, int height){
	FILE *fp = fopen(file_name,"w");
	if( fp==NULL){
		printf("File creation failed. %s ", file_name);
		return false;
	}
	p_file = fp;
	fprintf(p_file,"%d, %d\n", width, height);

	w=width;
	h = height;

	return true;
}



// we asume the line is in image coordinate
bool svgDrawer::pushLine(const line &obj_line){
	int x1, y1, x2, y2;
	
	float theta = (obj_line.orient/(float)num_orient)*PI;
	x1 = obj_line.x+obj_line.length*cos(theta)*0.5f+w*0.5f;
	y1 = obj_line.y-obj_line.length*sin(theta)*0.5f+h*0.5f;

	x2 = obj_line.x-obj_line.length*cos(theta)*0.5f+w*0.5f;
	y2 = obj_line.y+obj_line.length*sin(theta)*0.5f+h*0.5f;

	fprintf(p_file,"%d, %d, %d, %d, %d\n", 1,x1,y1,x2,y2);// 1 is the type code, denoting line
	
	
	return true;
}

bool svgDrawer::pushLineVertex(const lineVertex &obj_line){
	int x1,y1,x2,y2;
	x1 = obj_line.x_start + w*0.5f;
	y1 = obj_line.y_start + h*0.5f;

	x2 = obj_line.x_end + 0.5f*w;
	y2 = obj_line.y_end + 0.5f*h;
	fprintf(p_file,"%d, %d, %d, %d, %d\n", 1,x1,y1,x2,y2);// 1 is the type code, denoting line
	return true;

}


bool svgDrawer::saveImage(){
	if(!p_file){
		printf("file not created yet!");
		return false;
	}
	fclose(p_file);
	return true;
}
